Skip to content

feat: Add session variable support for all execution modes#58

Merged
evoludigit merged 1 commit into
devfrom
feature/session-variables-all-modes
Sep 16, 2025
Merged

feat: Add session variable support for all execution modes#58
evoludigit merged 1 commit into
devfrom
feature/session-variables-all-modes

Conversation

@evoludigit
Copy link
Copy Markdown
Contributor

Summary

Extends PostgreSQL session variable setting (app.tenant_id, app.contact_id) to all FraiseQL execution modes to enable consistent multi-tenant database access patterns.

Problem

Previously, session variables were only set in TurboRouter mode. When queries fell back to normal or passthrough modes, the session variables were not set, causing queries that rely on Row-Level Security (RLS) to fail.

This addresses the feature request documented in /tmp/fraiseql_session_variables_feature_request.md from the PrintOptim Backend Team.

Solution

  • Added _set_session_variables helper method to FraiseQLRepository
  • Integrated session variable setting in all database execution paths
  • Supports both psycopg (cursor) and asyncpg (connection) interfaces
  • Added comprehensive test coverage for all execution modes

Changes

  • src/fraiseql/db.py: Added _set_session_variables method and integrated it into all query execution paths
  • tests/integration/session/test_session_variables.py: New comprehensive test suite

Test Plan

✅ Added comprehensive test suite:

  • Tests for normal mode session variables
  • Tests for passthrough mode session variables
  • Tests for turbo mode session variables
  • Parametrized tests comparing all execution modes
  • Tests for conditional session variable setting
  • Tests for transaction scope (SET LOCAL)

✅ All existing tests pass:

  • 136 repository tests passing
  • 110 regression tests passing
  • No breaking changes

Impact

This enables Row-Level Security (RLS) and multi-tenant patterns to work consistently regardless of execution mode, fixing issues where queries would fail when falling back from TurboRouter to normal mode.

Before

  • ✅ TurboRouter mode: Sets session variables automatically
  • ❌ Normal mode: No session variables set
  • ❌ Passthrough mode: No session variables set

After

  • ✅ TurboRouter mode: Sets session variables automatically
  • ✅ Normal mode: Sets session variables automatically
  • ✅ Passthrough mode: Sets session variables automatically

Implementation Details

The implementation uses TDD methodology:

  1. RED Phase: Created failing tests for all execution modes
  2. GREEN Phase: Implemented minimal code to pass tests
  3. REFACTOR Phase: Extracted common logic to avoid duplication
  4. QA Phase: Verified all tests pass with no regressions

🤖 Generated with Claude Code

Extends PostgreSQL session variable setting (app.tenant_id, app.contact_id)
to all FraiseQL execution modes (normal, passthrough, turbo) to enable
consistent multi-tenant database access patterns.

Changes:
- Add _set_session_variables helper method to FraiseQLRepository
- Integrate session variable setting in all database execution paths
- Support both psycopg (cursor) and asyncpg (connection) interfaces
- Add comprehensive test coverage for session variables across modes

This enables Row-Level Security (RLS) and multi-tenant patterns to work
consistently regardless of the execution mode, addressing the issue where
queries would fail when falling back from TurboRouter to normal mode.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@evoludigit evoludigit merged commit 84571f9 into dev Sep 16, 2025
5 checks passed
@evoludigit evoludigit deleted the feature/session-variables-all-modes branch September 16, 2025 22:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant